#include "clock.h"
#include "ui_clock.h"

Clock::Clock(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Clock)
{
    ui->setupUi(this);
    this->setFixedSize(350, 250);

    connect(ui->clockButton, SIGNAL(clicked()), this, SLOT(enableClock()));
    connect(ui->chronoButton, SIGNAL(clicked()), this, SLOT(enableChrono()));
    connect(ui->resetButton, SIGNAL(clicked()), this, SLOT(resetChrono()));
    connect(ui->switchButton, SIGNAL(clicked()), this, SLOT(switchChrono()));
    connect(ui->hoursDecrementButton, SIGNAL(clicked()), this, SLOT(decrementHour()));
    connect(ui->minutesDecrementButton, SIGNAL(clicked()), this, SLOT(decrementMinute()));
    connect(ui->hoursIncrementButton, SIGNAL(clicked()), this, SLOT(incrementHour()));
    connect(ui->minutesIncrementButton, SIGNAL(clicked()), this, SLOT(incrementMinute()));

    // Timer used to update the clock
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(updateDisplay()));
    timer->setInterval(53);
    timer->start();

    enableClock();
}

Clock::~Clock()
{
    delete timer;
    delete ui;
}

void Clock::decrementHour()
{
    clock = clock.addSecs(-3600);
}

void Clock::decrementMinute()
{
    clock = clock.addSecs(-60);
}

void Clock::enableChrono()
{
    mode = kChronoStop;
    ui->hoursDecrementButton->setEnabled(false);
    ui->hoursIncrementButton->setEnabled(false);
    ui->minutesDecrementButton->setEnabled(false);
    ui->minutesIncrementButton->setEnabled(false);
    ui->resetButton->setEnabled(true);
    ui->switchButton->setEnabled(true);
    ui->chronoButton->setChecked(true);
    clock.setHMS(0, 0, 0);
}

void Clock::enableClock()
{
    mode = kClock;
    ui->hoursDecrementButton->setEnabled(true);
    ui->hoursIncrementButton->setEnabled(true);
    ui->minutesDecrementButton->setEnabled(true);
    ui->minutesIncrementButton->setEnabled(true);
    ui->resetButton->setEnabled(false);
    ui->switchButton->setEnabled(false);
    ui->clockButton->setChecked(true);
    clock = clock.currentTime();
}

void Clock::incrementHour()
{
    clock = clock.addSecs(3600);
}

void Clock::incrementMinute()
{
    clock = clock.addSecs(60);
}

void Clock::pauseChrono()
{
    mode = kChronoStop;
    ui->clockButton->setEnabled(true);
    ui->chronoButton->setEnabled(true);
    ui->resetButton->setEnabled(true);
    ui->switchButton->setText("Iniciar");

    QString hours, minutes, seconds, tenths;
    hours.setNum(ui->hoursNumber->value());
    minutes.setNum(ui->minutesNumber->value());
    seconds.setNum(ui->secondsNumber->value());
    tenths.setNum(ui->tenthsNumber->value());
    emit valueChanged(hours + ":" + minutes + ":" + seconds + ":" + tenths);
    // Another option would be
    // emit valueChanged(clock.toString("H:m:s:z"))
    // but that shows the miliseconds in z, instead of just tenths
    // and the rounding is a bit off
}

void Clock::resetChrono()
{
    clock.setHMS(0, 0, 0);
}

void Clock::startChrono()
{
    mode = kChrono;
    ui->clockButton->setEnabled(false);
    ui->chronoButton->setEnabled(false);
    ui->resetButton->setEnabled(false);
    ui->switchButton->setText("Pausar");
}

void Clock::switchChrono()
{
    if (mode == kChronoStop) {
        startChrono();
    } else if (mode == kChrono) {
        pauseChrono();
    }
}

void Clock::updateDisplay()
{
    if (mode != kChronoStop) {
        clock = clock.addMSecs(timer->interval());
    }

    ui->hoursNumber->display(clock.hour());
    ui->minutesNumber->display(clock.minute());
    ui->secondsNumber->display(clock.second());
    ui->tenthsNumber->display(clock.msec()/100);
}
